<?php

namespace app\worker;

use app\logic\Message;
use Codeages\Plumber\AbstractWorker;
use Codeages\Plumber\Queue\Job;

class SendWeChatWorker extends AbstractWorker
{
    public function execute(Job $job)
    {
        $this->logger->info("job ", [$job->getBody()]);
        $job->setDelay(100);

        $body = trim($job->getBody());

        try {
            list($templateId, $userOpenId) = explode(':', $body);
            $this->logger->info('Data', [$templateId, $userOpenId]);
            $this->getMessageLogic()->sendWeChatMessage($templateId, $userOpenId);

            return self::FINISH;
        } catch (\Exception $e) {
            $this->logger->error("execute job #{$job->getId()}", [$e->getMessage()]);
            throw $e;
            return self::BURY;
        }
    }

    /**
     * @return Message
     */
    protected function getMessageLogic(): Message
    {
        return app('logic')->logic('Message');
    }
}
